<?php

	namespace Phptricks\system\library\configuration;

	use Phptricks\system\core\library\library;
	use Phptricks\system\library\PhptricksExceptions\PhptricksExceptions as Exc;
	use Phptricks\system\library\loaders\loader as loaders;

	class configuration implements library{

		private static $configtype = 'php';
		public $_property;
		private static $_instance = null;

		public function __construct(){
			return $this;
		}

		public static function getinstance(){
			if (self::$_instance === null) {
				self::$_instance = new \Phptricks\system\library\configuration\configuration();
			}
			return self::$_instance;
		}

		public function _initialize(){
			
		}

		/**
		 * 
		 * @param type $configtype php or ini
		 * @return \Phptricks\system\library\configuration\configuration
		 */
		public function config_type($type = 'php'){
			self::$configtype = $type;
			$this->_getconfig();
			return $this->_property;
		}

		/**
		 * 
		 * @return \Phptricks\system\library\configuration\configuration
		 */
		private function _getconfig(){
			switch (\strtolower(self::$configtype)) {
				case 'ini':
					$this->_ini = IniScanner::useIniScanner();
					$this->_ini->Raed_Ini_File(\APPPATH . 'config/config.ini');
					$this->_property = (object) $this->_ini->ini_properties;
					break;

				case 'php':
					$config = loaders::getinstance()->_config('config');
					$this->_property = new \stdClass();
					$this->_set_properties($config);
					break;
			}
		}

		/**
		 * 
		 * @param type $configname
		 */
		public function getproperty($configname = ''){

			if (is_array($this->_property->$configname)) {
				return $this->_property->$configname;
			} else {
				throw new Exc("There is no variable by that name: '" . $configname . "'  You should make sure ");
			}
			throw new Exc("You must enter a value in param \$configname");
		}

		private function _set_properties(&$array_config){
			if (is_array($array_config) && count($array_config) > 0) {
				foreach ($array_config as $key => $value) {
					if (is_array($value)) {
						$this->_property->{$key} = (object) $value;
					} else {
						$this->_property->{$key} = $value;
					}
				}
			}
		}

	}
	